package com.hudongwx.sadpw.fragment.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.activity.FoodDetailActivity;
import com.hudongwx.sadpw.activity.TieZiDetailActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

/**
 * Author:Literary
 * DateTime:16-12-14 上午9:30
 */
public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.ViewHolder> {
    JSONArray recycle1;
    Activity activity;

    public MyRecycleAdapter(JSONArray recycle1, Activity activity) {
        this.recycle1 = recycle1;
        this.activity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = activity.getLayoutInflater().inflate(R.layout.item_home2, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        try {
            x.image().bind(holder.imageView,recycle1.getJSONObject(position).getString("CoverImg"));
            holder.textView1.setText(recycle1.getJSONObject(position).getString("Title"));
            holder.textView2.setText(recycle1.getJSONObject(position).getString("BoardName"));
            holder.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(v.getContext(), TieZiDetailActivity.class);
                    try {
                        int i = recycle1.getJSONObject(position).getInt("Id");
                        intent.putExtra("type",i);
                        activity.startActivity(intent);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if (null==recycle1)
            return 0;
        return recycle1.length();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView1;
        TextView textView2;
        RelativeLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            textView1= (TextView) itemView.findViewById(R.id.hotpoint_tv_1);
            textView2= (TextView) itemView.findViewById(R.id.hotpoint_tv_2);
            imageView= (ImageView) itemView.findViewById(R.id.hotpoint_iv_1);
            layout= (RelativeLayout) itemView.findViewById(R.id.hotpoint_rela);
        }
    }
}
